/*                                    _
 *   ___ _ __ ___ _ __ ___   ___   __| |
 *  / __| '__/ __| '_ ` _ \ / _ \ / _` |
 *  \__ \ |  \__ \ | | | | | (_) | (_| |
 *  |___/_|  |___/_| |_| |_|\___/ \__,_|     
 *    
 *    _|_ _  _ |    _    _ | _ ._|_
 *     | (_|| ||<  (/_><|_)|(_)| | 
 *                      |          
 *     _ _|__|_ _  _|    _ _  _  _|   | _ 
 *    (_| |  | (_|(_|<  | | |(_)(_||_||(/_
 *
 ******************************************
 ******************************************
 ******************************************
 * . _ _  _ | _  _ _  _  _ _|_ _ _|_. _  _ 
 * || | ||_)|(/_| | |(/_| | | (_| | |(_)| |
 *       |                                 
 *		
 *		Catches the Tank Spawn event to determine the current tank player id
 *		Uses left4downtown2 detour to block Rock Throwing Ability if player is also standard attacking
 *		Also known as Haymaker
 */

#define MODULE_NAME "TankPunchRock"


static const		BUTTON_ATTACK			= IN_ATTACK;

static Handle:cvarExploitFixEnabled			= INVALID_HANDLE;

static bool:eventsHooked					= false;
static 		tankClient						= -1;

public TankPunchRock_OnModuleLoaded()
{
	cvarExploitFixEnabled = 		CreateConVar("srs_tank_punchrock_fix",		"1", 	"Enable Exploit fix for Punching and Rock at same time", SRS_CVAR_DEFAULT_FLAGS);

	_TE_OnModuleEnabled(); // default ON
	HookConVarChange(cvarExploitFixEnabled, _TE_FlipActivation);
}

public _TE_FlipActivation(Handle:convar, const String:oldValue[], const String:newValue[])
{
	if (ACTIVATION_FLIP_OFF_TO_ON)
	{
		_TE_OnModuleEnabled();
	}
	else if (ACTIVATION_FLIP_ON_TO_OFF)
	{
		_TE_OnModuleDisabled();
	}
}

static _TE_OnModuleEnabled()
{
	HookEvent("tank_spawn", _TP_TankSpawn_Event, EventHookMode_Post);
	eventsHooked = true;
}

static _TE_OnModuleDisabled()
{
	if (!IsPluginEnding() && eventsHooked)
	{
		UnhookEvent("tank_spawn", _TP_TankSpawn_Event, EventHookMode_Post);
		eventsHooked = false;
	}
}

public Action:_TE_TankSpawn_Event(Handle:event, const String:name[], bool:dontBroadcast)
{
	if (!GetConVarInt(cvarExploitFixEnabled) || !IsAllowedGameMode())
	{
		return;
	}
	
	tankClient = GetEventInt(event, "tankid");
}

public Action:L4D_OnCThrowActivate()
{
	if(GetConVarBool(cvarExploitFixEnabled)
	&& IsAllowedGameMode()
	&& tankClient >= CLIENT_VALID_FIRST
	&& !IsFakeClient(tankClient)
	&& GetClientButtons(tankClient) & BUTTON_ATTACK)
	{
		Debug_Print("Blocking potential Haymaker on Tank %N", tankClient);
		return Plugin_Handled;
	}
	return Plugin_Continue;
}

#undef MODULE_NAME
